<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/web/head.jsp"%>
</head>
<body>
	<div class="layui-collapse">
		<div class="layui-colla-item">
			<h2 class="layui-colla-title">投稿人信息管理</h2>
			<div class="layui-colla-content layui-show"
				style="margin-left: 280px; padding-top: 40px;">
				<form class="layui-form">
					<div class="layui-form-item">
						<div class="layui-inline">
							<div class="layui-form-mid">账号</div>
							<input type="text" name="account" autocomplete="off"
								class="layui-input" style="width: 160px;">
						</div>

						<div class="layui-inline">
							<div class="layui-form-mid">昵称</div>
							<input type="text" name="penName" autocomplete="off"
								class="layui-input" style="width: 160px;">
						</div>

						<div class="layui-inline">
							<div class="layui-form-mid">真实姓名</div>
							<div class="layui-input-inline">
								<input type="text" name="realName" autocomplete="off"
									class="layui-input" style="width: 160px;">
							</div>
						</div>
						<div class="layui-inline">
							<input class="layui-btn ayui-btn-radius layui-btn-sm" lay-submit
								lay-filter="search" value="搜索" style="width: 50px;" /> <input
								type="reset"
								class="layui-btn ayui-btn-radius layui-btn-primary layui-btn-sm"
								value="重置" /> <input type="button"
								class="layui-btn layui-btn-primary layui-btn-sm" value="添加" />
						</div>
					</div>
					<input type="hidden" name="pageIndex" value="1"><input
						type="hidden" name="pageLimit" value="8"> <input
						type="hidden" name="action" value="get">
				</form>
			</div>
		</div>
	</div>
	<!-- 	table -->
	<div style="margin-top: 0px;">
		<table class="layui-table">
			<thead>
				<tr>
					<th>序号</th>
					<th>头像</th>
					<th>昵称</th>
					<th>真实姓名</th>
					<th>账号</th>
					<th>手机号</th>
					<th>邮箱</th>
					<th>简介</th>
					<th>作品数</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		<div id="test1" style="text-align: right; padding-right: 30px;"></div>
	</div>

	<script type="text/javascript">
	
		formOnSubmit('search', 'json', '/WriterServlet', function(data) {
			var curr = $('input[name="pageIndex"]').val()
			var limit = $('input[name="pageLimit"]').val()
			// 渲染
			laypage.render({
				elem : 'test1',
				count : data.count,
				layout : [ 'count', 'prev', 'page', 'next', 'limit', 'refresh',
						'skip' ],
				curr : curr,
				limit : limit,
				limits : [ 8, 10, 12, 20 ],
				jump : function(obj, first) {
					$('input[name="pageIndex"]').val(obj.curr)
					$('input[name="pageLimit"]').val(obj.limit)
					if (!first) {
						refresh();
					}
				}
			});
			// 显示
			var html = "";
			var tpl = $('#tradd').html();
			$.each(data.list, function(i, dom) {
				var d = {
					id : i + 1 + (curr - 1) * limit,
					realName : dom.realName,
					penName : dom.penName,
					account : dom.account,
					phone : dom.phone,
					email : dom.email,
					message : dom.message,
					count : dom.count,
					image : dom.image
				}
				html += laytpl(tpl).render(d)
			})
			$('tbody').html(html)
		})

		function refresh() {
			$('input[value="搜索"]').click();
		}

		refresh();

		function del(account,count) {
			console.log(count)
			if(count=='0'){
				layer.confirm("您确定要删除账号为" + account + "的作者吗?", {
					title : false,
					closeBtn : 0
				}, function() {
					ajax('/WriterServlet', 'text', {
						account : account,
						action : "del"
					}, function(data) {
						if (data == 'success') {
							layer.msg('删除成功', {
								time : 1000
							}, function() {
								refresh()
							})
						}
					})
				})
			}else{
				layer.msg("作者还有作品，不允许删除", {
					time : 1000,
					icon : 5
				})
			}
			
		}

		function reset(account) {
			ajax('/WriterServlet', 'text', {
				account : account,
				action : "reset"
			}, function(data) {
				if (data == "success") {
					layer.msg("重置成功", {
						time : 1000
					})
				} else {
					layer.msg("重置失败", {
						time : 1000
					})
				}
			})
		}

		function rePassword(account) {
			layer.prompt({
				formType : 1,
				title : '请输入要修改的密码',
			}, function(value, index) {
				layer.confirm("确定修改密码为" + value + "吗?", function() {
					ajax('/WriterServlet', 'text', {
						account : account,
						password : value,
						action : "rePassword"
					}, function(data) {
						if (data == "success") {
							layer.msg("修改成功", {
								time : 1000
							})
						} else {
							layer.msg("修改失败", {
								time : 1000
							})
						}
					})
					layer.close(index);
				})
			});
		}
		// 		修改
		function updata(account) {
			layerOpen('作家信息修改', '/web/manager/writerUpdata.jsp?account='
					+ account, '400px', '570px', refresh);
		}
		// 		添加
		function openAdd() {
			layerOpen('新添作家账号', '/web/manager/writerAdd.jsp', '400px', '570px',
					refresh)
		}
		// 		修改头像
		function loadImage(account) {
			layerOpen('点击图片选择头像', '/web/manager/writerImage.jsp?account='
					+ account, '600px', '480px', refresh)
		}
		
		$('input[value="添加"]').click(openAdd);

		
	</script>

	<script type="text/html" id="tradd">
		<tr>
			<td>{{d.id}}</td>
			<td>
				<img alt="" src="/image/{{d.image}}" style="width:40px;" id="img">
			</td>
			<td>{{d.penName}}</td>
			<td>{{d.realName}}</td>
			<td>{{d.account}}</td>
			<td>{{d.phone}}</td>
			<td>{{d.email}}</td>
			<td>{{d.message}}</td>
			<td>{{d.count}}</td>
			<td>
				<a href="javascript:del('{{d.account}}','{{d.count}}');" class="layui-btn layui-btn-danger layui-btn-sm">删除</a>
				<a href="javascript:updata('{{d.account}}');" class="layui-btn layui-btn-sm">修改信息</a>
				<a href="javascript:reset('{{d.account}}');" class="layui-btn layui-btn-sm">重置密码</a>
				<a href="javascript:rePassword('{{d.account}}');" class="layui-btn layui-btn-sm">修改密码</a>
				<a href="javascript:loadImage('{{d.account}}');" class="layui-btn layui-btn-sm">修改头像</a>
			</td>
		</tr>
</script>
</body>
</html>